package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.Utils.MD5Utils;
import com.itheima.reggie.dao.EmployeeDao;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeServrce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao,Employee> implements EmployeeServrce {


    @Autowired
    EmployeeDao employeeDao;

    @Override
    public Map checkLogin(Employee emp) {
        Map resultMap = new HashMap();

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,emp.getUsername());
        Employee employee = employeeDao.selectOne(queryWrapper);

        //判断用户名是否存在
        if (employee == null){
            resultMap.put("msg","此用户不存在");
            return resultMap;
        }

        //如果查询到用户信息存在，就校验用户密码是否正确（md5加密/Bcrypt(加盐加密)
        String userInPutPassW = MD5Utils.code(emp.getPassword());
        //用户输入的密码与数据库中的密码进行equals对比
        if (!userInPutPassW.equals(employee.getPassword())){
            //如果输入的密码不等于数据库中密码就result密码错误
            resultMap.put("msg","密码错误");
            return resultMap;
        }

        //判断用户账户状态是否为禁用，0是禁用
        if (employee.getStatus()==0){
            resultMap.put("msg","账户被禁用");
            return resultMap;
        }

        //用户登陆成功
        resultMap.put("emp",employee);

        return resultMap;
    }

    @Override
    public IPage<Employee> querypage(Map map) {

        //条件构造器，帮你写SQL语句的
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //1.2构建SQL语句

        //like模糊查询map.get("name")!=null && !"".equals(map.get("name"))前是做判断，
        // Employee::getName,map.get("name"));是在Employee表中查找到name
        queryWrapper.like(map.get("name")!=null && !"".equals(map.get("name")),Employee::getName,map.get("name"));
        //查询分页查询方法，查询分页数据
        IPage<Employee> page = new Page<>(Long.parseLong(
                (String)map.get("page")),Long.parseLong((String)map.get("pageSize")));
        employeeDao.selectPage(page,queryWrapper);
        //返回查询结果
        return page;
    }
}
